﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using University.Workshop.Business;

namespace University.Workshop.Presenters.WaterPurchase
{
    public class PresenterModifyWaterPurchaseView
    {
        IModifyBasicView viewModifyBasic;
        Model.WaterPurchase modifyWaterPurchase;
        IModifyWaterPurchaseView view;

        public PresenterModifyWaterPurchaseView(IModifyWaterPurchaseView View, Model.WaterPurchase wp)
        {
            view = View;
            modifyWaterPurchase = new Model.WaterPurchase();
            modifyWaterPurchase = wp;    //Ver que onda con esto
        }


        public void GoStepTwo()
        {
            //revisarPlaca();


            view.ModifyBasic.ChargeDataToBasic();
            modifyWaterPurchase = view.ModifyBasic.WaterPurchaseToModify;
            if (!PassFirstStep())
                view.CallFirstPage();
            else
                view.CallSecondPage();
            ProcedureAfterCall();
        }

        public void ModifyWaterPurchase()
        {
            //revisarPlaca();

            view.ModifyBasic.ChargeDataToBasic();

            Model.Cistern cistern = new Model.Cistern { LicencePlate = view.ModifyBasic.WaterPurchaseToModify.LicencePlate };
            cistern = University.Workshop.Business.ManagerCistern.GetCisternByPlate(cistern);

            view.ModifyBasic.WaterPurchaseToModify.Capacity = cistern.Capacity.ToString();
            view.ModifyBasic.WaterPurchaseToModify.PersonLastNames = cistern.LastNameDriver;
            view.ModifyBasic.WaterPurchaseToModify.PersonNames = cistern.NameDriver;

            //Vista que recibe los la IU
            
            modifyWaterPurchase = view.ModifyBasic.WaterPurchaseToModify;
            if (!PassFirstStep())
            {
                view.CallFirstPage();
                ProcedureAfterCall();
            }
            else if (!PassSecondStep())
            {
                view.CallSecondPage();
                ProcedureAfterCall();
            }
            else
            {
                ManagerWaterPurchase.UpdateWaterPurchase(modifyWaterPurchase);
                view.SystemMessage("La compra fue actualizada exitosamente.");
                view.ReturnSearchWaterPurchase();
            }
        }

        private bool PassFirstStep()
        {
            try
            {
                //Model.WaterPurchase wp = new Model.WaterPurchase();

                //wp.LicencePlate = view.ModifyBasic.WaterPurchaseToModify.LicencePlate;//VER Q ONDA CONESTO
                //ManagerWaterPurchase.VerifyDataPerson(wp);  //VER Q ONDA CON ESTO

                return true;
            }
            catch (Exception ex)
            {
                view.ErrorMessage(string.Format(ex.Message, "Actualizar"));
                return false;
            }
        }

        private bool PassSecondStep()
        {
            try
            {
                Model.WaterPurchase wp = new Model.WaterPurchase();

                wp.DatePurchase = view.ModifyBasic.WaterPurchaseToModify.DatePurchase;
                wp.Amount = view.ModifyBasic.WaterPurchaseToModify.Amount;
                wp.TotalPrice = view.ModifyBasic.WaterPurchaseToModify.TotalPrice;
                wp.Courtesy = view.ModifyBasic.WaterPurchaseToModify.Courtesy;

                wp.Capacity = view.ModifyBasic.WaterPurchaseToModify.Capacity;

                wp.DateRegistry = view.ModifyBasic.WaterPurchaseToModify.DateRegistry;

                ManagerWaterPurchase.VerifyDataPurchase(wp);
                return true;
            }
            catch (Exception ex)
            {
                view.ErrorMessage(string.Format(ex.Message, "Actualizar"));
                return false;
            }
        }

        public void GoBackPage()
        {
            viewModifyBasic.GoBackPage();
        }

        public void GoNextPage()
        {
            viewModifyBasic.GoNextPage();
        }
        public void GoStepOne()
        {
            view.ModifyBasic.ChargeDataToBasic();
            modifyWaterPurchase = view.ModifyBasic.WaterPurchaseToModify;
            view.CallFirstPage();
            ProcedureAfterCall();
        }

        private void ProcedureAfterCall()
        {
            if (viewModifyBasic != null)
                modifyWaterPurchase = viewModifyBasic.WaterPurchaseToModify;
            viewModifyBasic = view.ModifyBasic;
            viewModifyBasic.WaterPurchaseToModify = modifyWaterPurchase;
            viewModifyBasic.Update();
        }

        public void FirstTime()
        {
            view.CallFirstPage();
            ProcedureAfterCall();
        }

        public void NotifyErrorMessage(string MessageValue)
        {
            view.ErrorMessage(MessageValue);
        }
        public void NotifySystemMessage(string MessageValue)
        {
            view.SystemMessage(MessageValue);
        }
    }
}
